package driving;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.model.po.Article;
import com.g2room.driving.util.DbUtil;
import com.g2room.driving.util.HtmlUtil;
import com.g2room.driving.util.ImageUtil;

public class HandleHtmlByJsonup {
	public static void main(String[] args) {
		// 去掉注释、style和script标签
//		handleCommonTag();
//		handleATag();
//		handleJxydtTag();
//		handleKcjqTag();
//		handleCzzjTag();
		
//		findHotArti();
		handleImgTag();
		DbUtil.closeConn();
	}
	
	public static void handleImgTag() {
		List<Article> articles = getDetails(null, "女人巷");
		System.out.println("处理的文章数："+articles.size());

		for(Article arti : articles) {
			Document doc = Jsoup.parse(arti.getContent());
			doc.getElementsByTag("img").remove();
			updateContent(arti.getId(), HtmlUtil.handleSpace(doc.body().html()));
		}
	}
	
	public static void handleATag() {
		List<Article> articles = getDetails(null, null);
		System.out.println("处理的文章数："+articles.size());

		for(Article arti : articles) {
			try{
				// 删除注释
				String html = HtmlUtil.delNoteTag(arti.getContent());
				// 删除js和style
				html = HtmlUtil.delScriptAndStyleTag(html);
				HandleContentByJsonup.updateContent(arti.getId(), handleATag(html));
	        }catch(Exception e) {
	        	e.printStackTrace();
	        }
		}
	}
	
	public static String handleATag(String html) {
		html=html.replaceAll("<a href[^>]*>", "").replaceAll("</a>", "");
		System.out.println(html);
		return html;
	}
	public static List<Article> getDetails(String condition, String source) {
		List<Article> articles = new ArrayList<Article>();
		try {
			Connection connection = DbUtil.getQgshConn();
			Statement statement = connection.createStatement();
			condition = (null == condition) ? condition = "" : "and content like '%"+condition+"%' "; 
			source = (null == source) ? source = "" : "and source='"+source+"' ";
			String sql="select id,category_id, category_code, type_code, type_id,title,content from d_article_grap "
					+ "where 1=1 " + source + condition;
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				Article article = new Article();
				article.setId(rs.getLong("id"));
				article.setContent(rs.getString("content"));
				article.setCategoryCode(rs.getString("category_code"));
				article.setTypeCode(rs.getString("type_code"));
				if(null == article.getTypeCode() || article.getTypeCode().length()==0) {
					article.setTypeCode(article.getCategoryCode());
				}
				articles.add(article);
			}
			rs.close();
			statement.close();
//			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}

	public static void updateContent(long id, String content) {
		try {
			Connection connection = DbUtil.getQgshConn();
			PreparedStatement statement = connection.prepareStatement("update d_article_grap set content =? where id=?");
			statement.setString(1, content);
			statement.setLong(2, id);
			statement.execute();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
